﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Qplus.beans;
using System.Diagnostics;
using Qplus.Infrastructure;
using Qplus.protocol;
using Qplus.enums;

namespace Qplus.UI
{
    public class LoginLogout:IPacketListener
    {
        String version = "1000";
        static LoginLogout instance;

        public event EventHandler<UIEvent> LoginEvent;
        public event EventHandler<UIEvent> LogoutEvent;

        public LoginLogout()
        {
            //PacketQueue queue = new PacketQueue();
            //queue.addPacketListener(RoomListener.Instance);
        }

        public static LoginLogout Instance
        {
            get
            {
                if (null == instance)
                    instance = new LoginLogout();
                return instance;
            }
        }

        public void onLoginVerify(int result, string ip, int port,
            byte[] key, long userID, String upurl)
        {
            switch (result)
            {
                case 0:
                        QUser.Instance.key = key;
                        //user.setKey(key);
                        QUser.Instance.getInfo().setUserID(userID);
                        QUser.Instance.url = upurl;
                        QUser.Instance.userID = userID;
                    QplusT.getInstance().loginUserServer(ip, port, version, "", key, userID);
                    Debug.WriteLine("验证成功");
                    break;
                case 1:
                    QplusT.changeLoginState(QUser.Instance, false, false);
                    //			if(user != null){
                    //				user.setKey(null);
                    //				user.setUserID(-1);
                    //				user.notifyObservers();
                    //			}
                    ConnectionManager.getInstance().closeConnection();
                    Debug.WriteLine("", "验证失败");
                    break;

                default:
                    break;
            }
        }

        public void onLoginUserServer(int result, long appID, E_USER_LEVEL level)
        {
            switch (result)
            {
                case 0:
                    QUser.Instance.appID = appID;
                    QUser.Instance.getInfo().setUserLevel(level);
                    //user.getInfo().notifyObservers();
                    QplusT.changeLoginState(QUser.Instance, false, true);
                    Debug.WriteLine("登陆成功");
                    QUser.Instance.setForceLogout(false);
                    processLoginUserServer();
                    break;
                case 1:
                    QplusT.changeLoginState(QUser.Instance, false, false);

                    Debug.WriteLine("登录失败");
                    break;

                default:
                    break;
            }
        }

        public void onLogout(int result)
        {
            ConnectionManager.getInstance().closeConnection();
            QUser user = QplusT.getInstance().getUser();
            QplusT.changeLoginState(user, false, false);
            KeepAlive.getInstance().stopKeepAlive();
        }

        public virtual void packetEvent(PacketEvent Pevent)
        {
            InPacket packet = Pevent.myPacket;
            if (packet == null)
            {
                return;
            }
            //验证
            else if (packet is L2C_RESP_LOGIN)
            {
                L2C_RESP_LOGIN p = ((L2C_RESP_LOGIN)packet);
                onLoginVerify(p.getResult(), p.getIP(),
                            p.getPort(), p.getKey(), p.getUserID(),
                            p.getUpgradeURL());
            }
            //登陆用户服务器
            else if (packet is U2C_RESP_LOGIN)
            {
                U2C_RESP_LOGIN p = ((U2C_RESP_LOGIN)packet);
                E_USER_LEVEL level = 0;
                int l = p.getUserLevel();
                if (l == (int)E_USER_LEVEL.EUL_GENERAL)
                {
                    level = E_USER_LEVEL.EUL_GENERAL;
                }
                else if (l == (int)E_USER_LEVEL.EUL_SENIOR)
                {
                    level = E_USER_LEVEL.EUL_SENIOR;
                }
                else if (l == (int)E_USER_LEVEL.EUL_CS)
                {
                    level = E_USER_LEVEL.EUL_CS;
                }
                onLoginUserServer(p.getResult(), p.getAppID(),
                            level);
            }
            //退出登录
            else if (packet is U2C_RESP_LOGOUT)
            {
                U2C_RESP_LOGOUT p = ((U2C_RESP_LOGOUT)packet);
                onLogout(p.getResult());
                processLogoutUserServer();
            }
        }

        private void processLoginUserServer()
        {
            //if(null!=onStateChanged)
            //    onStateChanged(this, new UIEvent("ProcessIncomingMessage"));
            if (null != LoginEvent)
                LoginEvent(this, new UIEvent());
        }
        private void processLogoutUserServer()
        {
            //if(null!=onStateChanged)
            //    onStateChanged(this, new UIEvent("ProcessIncomingMessage"));
            if (null != LogoutEvent)
                LogoutEvent(this, new UIEvent());
        }
    }
}
